package com.ruiysoft;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.net.ssl.SSLContext;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.KeyStore;

public class HttpHelper {
    public String httpsSSLPostData(String url, String data) throws Exception {
        StringBuffer out = new StringBuffer();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        Resource resource = new ClassPathResource("apiclient_cert.p12");
//        FileInputStream instream = new FileInputStream(resource.getInputStream());//放退款证书的路径
        try {
            keyStore.load(resource.getInputStream(), "1371893102".toCharArray()); // 1371893102 指的是商户号
        } finally {
//            instream.close();
        }

        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1371893102".toCharArray()).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                new String[]{"TLSv1"},
                null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

        HttpPost httpPost = new HttpPost(url);//退款接口
        System.out.println("executing request" + httpPost.getRequestLine());

        StringEntity reqEntity  = new StringEntity(data);
        // 设置类型
        reqEntity.setContentType("application/x-www-form-urlencoded");
        httpPost.setEntity(reqEntity);
        CloseableHttpResponse response = httpclient.execute(httpPost);

        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
            String text;

            while ((text = bufferedReader.readLine()) != null) {
                System.out.println(text);
                out.append(text);
            }
        }

        EntityUtils.consume(entity);
        response.close();
        httpclient.close();

        return out.toString();
    }

	public String getData(String url) throws Exception {
		String result = "";
        HttpClient client = new DefaultHttpClient();  
        HttpGet request = new HttpGet(url);
          
        HttpResponse response = client.execute(request);
          
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String line = "";  
        while((line = rd.readLine()) != null) {
        	result += line;
        }
        
		return result;
	}
	
	public String postData(String url) throws Exception {
		String result = "";
        HttpClient client = new DefaultHttpClient();  
        HttpPost request = new HttpPost(url);
            
        HttpResponse response = client.execute(request);
          
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));  
        String line = "";  
        while((line = rd.readLine()) != null) {
        	result += line + "\r\n";
        }
        
		return result;
	}
	
	public String postData(String url, String data) throws Exception {
        System.out.println("url: " + url);
        System.out.println("data: " + data);
        String result = "";
        HttpClient client = new DefaultHttpClient();  
        HttpPost request = new HttpPost(url);
        HttpEntity entity = new StringEntity(data, "utf-8");
        request.setEntity(entity);
            
        HttpResponse response = client.execute(request);
          
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));  
        String line = "";  
        while((line = rd.readLine()) != null) {
        	result += line + "\r\n";
        }
        
		return result;
	}
}
